<?php

class ShipPlan extends Wp_ship_plan_model {

    function __construct() {
        parent::__construct();
    }
        
    function getList($filter = array()) {
        $ship_plan = new ShipPlan();
        if(isset($filter['name']) && $filter['name']) {
            $ship_plan->addWhere("wp_ship_plan.ship_name LIKE '%".$filter['name']."%'");
        }
        if(isset($filter['country']) && $filter['country']) {
            $ship_plan->addWhere("wp_ship_plan.id_wp_country = ".intval($filter['country']));
        }
        if(isset($filter['start_date']) && $filter['start_date']) {
            $ship_plan->addWhere("wp_ship_plan.date_time >= '".date_to_date_sql($filter['start_date'])."'");
        }
        if(isset($filter['end_date']) && $filter['end_date']) {
            $ship_plan->addWhere("wp_ship_plan.date_time <= '".date_to_date_sql($filter['end_date'])."'");
        }
        if(isset($filter['region']) && $filter['region']) {
            $port->addWhere("wp_ship_plan.id_wp_region = ".intval($filter['region']));
        }
        $ship_plan->addJoin(new Country());
        $ship_plan->addJoin(new Purposes(), 'LEFT');
        $ship_plan->addJoin(new Region(), 'LEFT');
        $ship_plan->addSelect();
        $ship_plan->addSelect("wp_ship_plan.*, wp_country.name as country_name, wp_purposes.name as purposes, wp_region.name as region_name");
        $ship_plan->orderBy("wp_ship_plan.date_time DESC, wp_ship_plan.ship_name");
        $ship_plan->find();
        return $ship_plan;            
    }
    
    function validateInput() {

        if (!$this->id_wp_country) {
            MessageHandler::add (_MSG_COUNTRY_NOT_EMPTY, MSG_ERROR, LOG_ONLY);
        } else {
            $country = new Country();
            if (!$country->get($this->id_wp_country)) {
                 MessageHandler::add (_MSG_COUNTRY_NOT_EMPTY, MSG_ERROR, LOG_ONLY);
            }
        }
        
        if (empty($this->id_wp_region)) {
            MessageHandler::add (_MSG_REGION_NOT_EMPTY, MSG_ERROR, LOG_ONLY);
        } else {
            $region = new Region();
            if (!$region->get($this->id_wp_region)) {
                 MessageHandler::add (_MSG_REGION_INVALID, MSG_ERROR, LOG_ONLY);
            }
        }
        
        if (empty ($this->ship_name)) {
            MessageHandler::add (_MSG_SHIP_NAME_NOT_EMPTY, MSG_ERROR, LOG_ONLY);
        } else {
            if (strlen($this->ship_name) > MAX_LENGTH_NAME) {
                MessageHandler::add (_MSG_SHIP_NAME_IS_TOO_LONG, MSG_ERROR, LOG_ONLY);
            }
            
            if (self::isExistsByName($this->id, $this->ship_name, $this->date_time)) {
                MessageHandler::add (_MSG_SHIP_NAME_EXISTS, MSG_ERROR, LOG_ONLY);
            }
        }
        
        if (empty ($this->date_time)) {
            MessageHandler::add (_MSG_DATE_TIME_NOT_EMPTY, MSG_ERROR, LOG_ONLY);
        } else {
            if(!isValidDate($this->date_time)) {
                MessageHandler::add (_MSG_DATE_TIME_INVALID, MSG_ERROR, LOG_ONLY);
            }
            $this->date_time = date_to_date_sql($this->date_time);
        }
        
        if (empty($this->draf)) {
            MessageHandler::add (_MSG_DRAF_NOT_EMPTY, MSG_ERROR, LOG_ONLY);
        } else {
            if (!is_numeric($this->draf)) {
                MessageHandler::add (_MSG_DRAF_INVALID, MSG_ERROR, LOG_ONLY);
            }
        }
        
        if (empty($this->loa)) {
            MessageHandler::add (_MSG_LOA_NOT_EMPTY, MSG_ERROR, LOG_ONLY);
        } else {
            if (!is_numeric($this->loa)) {
                MessageHandler::add (_MSG_LOA_INVALID, MSG_ERROR, LOG_ONLY);
            }
        }
        
        if ($this->dwt && !is_numeric($this->dwt)) {
            MessageHandler::add (_MSG_DWT_INVALID, MSG_ERROR, LOG_ONLY);
        }
        
        if (empty ($this->goods)) {
            MessageHandler::add (_MSG_GOODS_NOT_EMPTY, MSG_ERROR, LOG_ONLY);
        }
        
        if (empty ($this->id_wp_purposes)) {
            MessageHandler::add (_MSG_PURPOSE_NOT_EMPTY, MSG_ERROR, LOG_ONLY);
        } else {
            $purposes = new Purposes();
            if (!$purposes->get($this->id_wp_purposes)) {
                 MessageHandler::add (_MSG_PURPOSES_INVALID, MSG_ERROR, LOG_ONLY);
            }
        }
        
        return MessageHandler::countError() > 0 ? false : true;

    }
    
    public function isExistsByName($id = 0, $name = '', $date_time = '')
	{
		if ($name == '' || $date_time == '')
		{
			return false;
		}	
		$ship_plan = new ShipPlan();
		
        if ($id) {
			$ship_plan->addWhere("id <> {$id}");
		}
                
        $ship_plan->addWhere("ship_name = '$name'");
		$ship_plan->addWhere("date_time = '$date_time'");
		$ship_plan->find();
		return $ship_plan->countRows() > 0;
	}
    
    public function insert() {
        $this->created_date = date('Y-m-d H:i:s');
        parent::insert();
    }
    
    public function update() {
        $this->modified_date = date('Y-m-d H:i:s');
        parent::update();
    }
}
